package com.msb.mall.ware.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.msb.common.dto.WareHasStockDTO;
import com.msb.common.exception.BizCodeEnume;
import com.msb.mall.ware.vo.OrderItemVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.msb.mall.ware.entity.WareSkuEntity;
import com.msb.mall.ware.service.WareSkuService;
import com.msb.common.utils.PageUtils;
import com.msb.common.utils.R;



/**
 * 商品库存
 *
 * @author dpb
 * @email dengpbs@163.com
 * @date 2022-10-08 23:00:45
 */
@RestController
@RequestMapping("ware/waresku")
public class WareSkuController {
    @Autowired
    private WareSkuService wareSkuService;

    /**
     * 下订单成功，锁定库存
     * @param checkCartItems
     */
    @PostMapping("/lockStock")
    public R toLockStock(@RequestBody List<OrderItemVo> checkCartItems){
        Boolean b = wareSkuService.lockStock(checkCartItems);
        if (b){
            return R.ok();
        }else {
            return R.error(BizCodeEnume.NO_STOCK_EXCEPTION.getMsg());
        }
    }

    /**
     * 根据skuId集合批量查询是否有库存
     * @param skuIds
     * @return
     */
    @PostMapping("/isHasStock")
    public R isHasStock(@RequestBody List<Long> skuIds){

        List<WareHasStockDTO> hasStock = wareSkuService.isHasStock(skuIds);
        hasStock.stream().collect(Collectors.toMap(WareHasStockDTO::getSkuId,WareHasStockDTO::getHasStock));
        return R.ok().put("hasStockList",hasStock);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("ware:waresku:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = wareSkuService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("ware:waresku:info")
    public R info(@PathVariable("id") Long id){
		WareSkuEntity wareSku = wareSkuService.getById(id);

        return R.ok().put("wareSku", wareSku);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("ware:waresku:save")
    public R save(@RequestBody WareSkuEntity wareSku){
		wareSkuService.save(wareSku);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("ware:waresku:update")
    public R update(@RequestBody WareSkuEntity wareSku){
		wareSkuService.updateById(wareSku);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("ware:waresku:delete")
    public R delete(@RequestBody Long[] ids){
		wareSkuService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
